<?php

namespace App\Controllers;

class Doctor extends BaseController {

    public function getIndex() {
        // connect to the model
        $places = new \App\Models\Doctor();
        // retrieve all the records
        $records = $places->findAll();


        $table = new \CodeIgniter\View\Table();
        $table->setHeading(["Name","Description","Image"]);
        foreach ($records as $record) {
            $nameLink = anchor("doctor/showme/$record->id", $record->name);
            $image = new \App\Models\Doctor();
            $table->addRow($nameLink, $record->description, "<img height='300' width='250' src='/image/$record->image'/>");
        }
        $template = [
            'table_open' => '<table cellpadding="5px">',
            'cell_start' => '<td style="border: 1px solid #dddddd;">',
            'row_alt_start' => '<tr style="background-color:#dddddd">',
        ];
        $table->setTemplate($template);
        $fields = [
            'title' => 'Ancient Chinese doctors',
            'heading' => 'Ancient Chinese doctors',
            'footer' => 'Copy right BeiPengLin'
        ];

        // get a template parser
        $parser = \Config\Services::parser();
        // tell it about the substitions
        return $parser->setData($fields)
                ->render('templates\top') .
            $table->generate() .
            $parser->setData($fields)
                ->render('templates\bottom');
    }

    public function getShowme($id) {
        // connect to the model
        $places = new \App\Models\Doctor();
        // retrieve all the records
        $record = $places->find($id);
        // get a template parser
        $parser = \Config\Services::parser();
        // tell it about the substitions

        $table= new \CodeIgniter\View\Table();
        $table->addRow('id',$record['id']);
        $table->addRow('name',$record['name']);
        $table->addRow('description',$record['description']);
        $table->addRow('city',$record['city']);
        $table->addRow('book',$record['book']);
        $table->addRow('link',$record['link']);
        $table->addRow('image','<img src="/image/'.$record['image'].'"/>');
        $template = [
            'table_open' => '<table cellpadding="5px">',
            'cell_start' => '<td style="border: 1px solid #dddddd;">',
            'row_alt_start' => '<tr style="background-color:#dddddd">',
        ];
        $table->setTemplate($template);

        $fields = [
            'title' => 'Ancient Chinese doctors',
            'heading' => 'Ancient Chinese doctors',
            'footer' => 'Copy right Bei Peng Lin'
        ];

        return $parser->setData($fields)
                ->render('templates\top') .
            $table->generate() .
            $parser->setData($fields)
                ->render('templates\bottom');
    }
}
